(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.popup.ppfMemberEditController', ppfMemberEditController);
	ppfMemberEditController.$inject = [ '$scope', '$q', '$uibModalInstance', '$timeout', 'Constants', 'dialogsModule', 'gridService', 'modalService', 'layoutConfig' , 'authService', 'ajaxServiceFactory', 'validatorConfig', 'ppfMemberService'];
	function ppfMemberEditController($scope, $q, $uibModalInstance, $timeout, Constants, dialogsModule, gridService, modalService, layoutConfig, authService, ajaxServiceFactory, validatorConfig, ppfMemberService) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("ppfMemberEditController"), ajaxService = ajaxServiceFactory.buildAjaxService({
		});
		ctrl.dialogsModule = dialogsModule;
//		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.initValidatorConfig = angular.copy(ctrl.validatorConfig);
		ctrl.validatorConfig = {};
		ctrl.filterPrimaryKeys = ctrl.dialogsModule.params.filterPrimaryKeys || [];
		ctrl.isEdit = ctrl.dialogsModule.params.isEdit;
		ctrl.updateProcessFlag = ctrl.dialogsModule.params.updateProcessFlag;
		ctrl.ppfCommunityBuyerCode = ctrl.dialogsModule.params.ppfCommunityBuyerCode;
		ctrl.formName = "ppfMemberForm";
		ctrl.ppfMember = {};
		
		ctrl.init = function() {
			// init acct
			ppfMemberService.initProfAcct(layoutConfig, $scope, ctrl, ctrl.updateProcessFlag);
			// init int
			//ppfMemberService.initProfInt(layoutConfig, $scope, ctrl, ctrl.updateProcessFlag);
			//ppfMemberService.initProfCostOfFundInt(layoutConfig, $scope, ctrl, ctrl.updateProcessFlag);
			// init pastdue
			ppfMemberService.initProfPastdue(layoutConfig, $scope, ctrl, ctrl.updateProcessFlag);
			// init fee
			ppfMemberService.initProfFee(layoutConfig, $scope, ctrl, ctrl.updateProcessFlag);
			// init doc
			ppfMemberService.initEventDoccfg(layoutConfig, $scope, ctrl, ctrl.updateProcessFlag);
			// init limit facility
			//ppfMemberService.initProfLimitFaci(layoutConfig, $scope, ctrl, ctrl.updateProcessFlag);
			//ppfMemberService.initProfLimitInsFaci(layoutConfig, $scope, ctrl, ctrl.updateProcessFlag);
			//ppfMemberService.initProfLimitPurFaci(layoutConfig, $scope, ctrl, ctrl.updateProcessFlag);
			// init Settlement Instruction
			ppfMemberService.initProfSetlinst(layoutConfig, $scope, ctrl, ctrl.updateProcessFlag);
			// init cust prop define
			ppfMemberService.initCustProp(layoutConfig, $scope, ctrl, ctrl.updateProcessFlag);
			//
			ppfMemberService.initGeneral($scope, ctrl, ctrl.updateProcessFlag).then(function() {
				if (ctrl.dialogsModule.params.isAdd == 'Y') {
					authService.initPpfMember().then(function(response) {
						var initPpfMember = angular.merge(response.data, ctrl.dialogsModule.params.ppfMember || {});
						initPpfMember.prodSubtypeExtProperty = ctrl.dialogsModule.params.prodSubtypeExtProperty;
						initPpfMember.prodSubtype = initPpfMember.prodSubtypeExtProperty.prodSubtype;
						// reset data
						ppfMemberService.resetMemberByProdSubtypeExtProperty(ctrl, initPpfMember);
						ppfMemberService.initLayoutConfigAndModel(ctrl, $scope, initPpfMember, "ppfMemberEdit", ctrl.updateProcessFlag).then(function() {
							// reset cust prop data
							ctrl.resetCustProperties();
						});
					}, angular.noop);
				} else {
					var initPpfMember = ctrl.dialogsModule.params.ppfMember || {};
					initPpfMember.prodSubtypeExtProperty = ctrl.dialogsModule.params.prodSubtypeExtProperty;
					initPpfMember.prodSubtype = initPpfMember.prodSubtypeExtProperty.prodSubtype;
					// reset data
					ppfMemberService.resetMemberByProdSubtypeExtProperty(ctrl, initPpfMember);
					ppfMemberService.initLayoutConfigAndModel(ctrl, $scope, initPpfMember, "ppfMemberEdit", ctrl.updateProcessFlag).then(function() {
						// reset cust prop data
						ctrl.resetCustProperties();
					});
				}
			});
		};
		
		ctrl.ok = function() {
			ppfMemberService.clearDirtyData(ctrl);
			$timeout(function(){
				if ($scope.ppfMemberForm) {
					$scope.ppfMemberForm.$setSubmitted();
					if(!$scope.ppfMemberForm.$valid) {
						dialogsModule.showErrorMessages($scope.ppfMemberForm);
						return;
					}
				}
				if ((!$scope.ppfMemberForm || $scope.ppfMemberForm.$valid) && (!dialogsModule.btnCallback || !dialogsModule.btnCallback['ok'] || dialogsModule.btnCallback['ok'](ctrl.ppfMember) !== false)) {
					$uibModalInstance.close();
				}
			});
		};
		
		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['close'] || dialogsModule.btnCallback['close']() !== false) {
				$uibModalInstance.dismiss();
			}
		};
		
	}
})(angular);
